package com.example.lab.controller;



import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.lab.entity.Admin;
import com.example.lab.entity.MyReturn;
import com.example.lab.entity.User;
import com.example.lab.service.AdminService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (Admin)表控制层
 *
 * @author makejava
 * @since 2022-04-22 22:44:50
 */
@RestController
@RequestMapping("admin")
public class AdminController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private AdminService adminService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param admin 查询实体
     * @return 所有数据
     */
    @GetMapping("selectAll")
    public MyReturn selectAll(Integer limit, Integer page, Admin admin) {
        Page<Admin> adminPage= new Page<>(page,limit);
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(admin.getAname())){
            queryWrapper.like("Aname",admin.getAname());
        }
        Page<Admin> page1=this.adminService.page(adminPage,queryWrapper);
        return super.success(page1.getRecords(),page1.getTotal());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public MyReturn selectOne(@PathVariable Serializable id) {
        return success(this.adminService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param admin 实体对象
     * @return 新增结果
     */

    @PostMapping("insert")
    public  MyReturn insert(@RequestBody Admin admin) {
        boolean save= this.adminService.save(admin);
        if(save){
            return  super.success(admin,1L);
        }else {
            return super.failed(admin,1L);
        }

    }

    /**
     * 修改数据
     *
     * @param admin 实体对象
     * @return 修改结果
     */
    @PutMapping("update")
    public  MyReturn update(@RequestBody Admin admin) {
        boolean update=this.adminService.updateById(admin);
        if(update){
            return  super.success(admin,1L);
        }else {
            return  super.failed(admin,1L);
        }
    }
    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("delete")
    public  MyReturn delete(@RequestParam("idList") List<Long> idList) {
        boolean b=adminService.removeByIds(idList);
        if(b){
            return super.success(idList);
        }else {
            return super.failed(idList);
        }
    }
    @PostMapping("logina")
    public  MyReturn login(String loginName,String password) {
        if(loginName=="" || password==""){
            return  failed("用户名或密码不能为空");
        }else {
            QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
            if(ObjectUtil.isNotEmpty(loginName)){
                queryWrapper.eq("Auser",loginName);
            }
            if(ObjectUtil.isNotEmpty(password)){
                queryWrapper.eq("Apassword",password);
            }
            List<Admin> select = adminService.list(queryWrapper);
            if(select.size()>0){
                return success(null);
            }else {
                return failed("用户名或密码错误");
            }
        }


    }
}

